English | 简体中文
Alibaba Cloud Credentials for TypeScript/Node.js
Installation
npm install @alicloud/credentials
Node.js >= 12 required.
Quick Examples
Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.
Credential Type
access_key
Setup access_key credential through User Information Management, it have full authority over the account, please keep it safe. Sometimes for security reasons, you cannot hand over a primary account AccessKey with full access to the developer of a project. You may create a sub-account RAM Sub-account , grant its authorization,and use the AccessKey of RAM Sub-account.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'access_key',
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
type
} = await cred.getCredential();
sts
Create a temporary security credential by applying Temporary Security Credentials (TSC) through the Security Token Service (STS).
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'sts',
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
securityToken: 'securityToken',
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
ram_role_arn
By specifying RAM Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy
.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'ram_role_arn',
accessKeyId: 'accessKeyId',
accessKeySecret: 'accessKeySecret',
roleArn: 'roleArn',
roleSessionName: 'roleSessionName',
policy: 'policy',
roleSessionExpiration: 3600,
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
oidc_role_arn
By specifying OIDC Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy
.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'oidc_role_arn',
roleArn: 'roleArn',
oidcProviderArn: 'oidcProviderArn',
oidcTokenFilePath: '/Users/xxx/xxx',
roleSessionName: 'roleSessionName',
policy: 'policy',
roleSessionExpiration: 3600,
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
ecs_ram_role
By specifying the role name, the credential will be able to automatically request maintenance of STS Token.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'ecs_ram_role',
roleName: 'roleName',
disableIMDSv1: true,
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
rsa_key_pair
By specifying the public key ID and the private key file, the credential will be able to automatically request maintenance of the AccessKey before sending the request. Only Japan station is supported.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'rsa_key_pair',
privateKeyFile: 'privateKeyFile',
publicKeyId: 'publicKeyId',
}
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
credentials_uri
By specifying a local or remote URI to get credentials and refresh automanticly.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'credentials_uri',
credentialsURI: 'http://a_local_or_remote_address/'
};
const cred = new Credential(config);
const {
accessKeyId,
accessKeySecret,
securityToken,
type
} = await cred.getCredential();
The URI must reponse meets following conditions:
- response status code is 200
- response body struct must be:
{
"Code": "Success",
"AccessKeySecret": "AccessKeySecret",
"AccessKeyId": "AccessKeyId",
"Expiration": "2021-09-26T03:46:38Z",
"SecurityToken": "SecurityToken"
}
bearer
If credential is required by the Cloud Call Centre (CCC), please apply for Bearer Token maintenance by yourself.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'bearer',
bearerToken: 'bearerToken',
}
const cred = new Credential(config);
let bearerToken: string = await cred.getBearerToken();
let type: string = cred.getType();
Provider
If you call new Credential()
with empty, it will use provider chain to get credential for you.
1. Environment Credentials
The program first looks for environment credentials in the environment variable. If the ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
environment variables are defined and are not empty, the program will use them to create the default credential. If not, the program loads and looks for the client in the configuration file.
2. Config File
If there is ~/.alibabacloud/credentials
default file (Windows shows C:\Users\USER_NAME\.alibabacloud\credentials
), the program will automatically create credential with the name of 'default'. The default file may not exist, but a parse error throws an exception. The specified files can also be loaded indefinitely: AlibabaCloud::load('/data/credentials', 'vfs://AlibabaCloud/credentials', ...);
This configuration file can be shared between different projects and between different tools. Because it is outside the project and will not be accidentally committed to the version control. Environment variables can be used on Windows to refer to the home directory %UserProfile%. Unix-like systems can use the environment variable $HOME or ~ (tilde). The path to the default file can be modified by defining the ALIBABA_CLOUD_CREDENTIALS_FILE
environment variable.
[default]
type = access_key
access_key_id = foo
access_key_secret = bar
3. Instance RAM Role
If the environment variable ALIBABA_CLOUD_ECS_METADATA
is defined and not empty, the program will take the value of the environment variable as the role name and request http://100.100.100.200/latest/meta-data/ram/security-credentials/
to get the temporary Security credential.
4. Credentials URI
If the environment variable ALIBABA_CLOUD_CREDENTIALS_URI
is defined and not empty,
the program will take the value of the environment variable as the credentials uri.
Test & Coverage
npm run test
npm run cov
License
MIT
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.